import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../controller/home_controller.dart';
import '../../../core/state/universal_state_wrapper.dart';
import '../../../core/state/page_state.dart';

class HomePage extends GetView<HomeController> {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('GetX MVVM 示例'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        actions: [
          Obx(() => IconButton(
            onPressed: controller.isRefreshing ? null : controller.refreshTodos,
            icon: controller.isRefreshing
                ? const SizedBox(
                    width: 20,
                    height: 20,
                    child: CircularProgressIndicator(strokeWidth: 2),
                  )
                : const Icon(Icons.refresh),
            tooltip: '刷新',
          )),
        ],
      ),
      body: Obx(() => UniversalStateWrapper(
        state: controller.pageState,
        errorMessage: controller.errorMessage,
        onRetry: controller.retryLoad,
        child: RefreshIndicator(
          onRefresh: controller.refreshTodos,
          child: ListView.builder(
            physics: const AlwaysScrollableScrollPhysics(),
            itemCount: controller.todos.length,
            itemBuilder: (context, index) {
              final todo = controller.todos[index];
              return Card(
                margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
                child: ListTile(
                  leading: Checkbox(
                    value: todo.completed,
                    onChanged: (_) => controller.toggleTodoStatus(index),
                  ),
                  title: Text(
                    todo.title,
                    style: TextStyle(
                      decoration: todo.completed 
                          ? TextDecoration.lineThrough 
                          : null,
                      color: todo.completed 
                          ? Colors.grey 
                          : null,
                    ),
                  ),
                  subtitle: Text('ID: ${todo.id}'),
                  trailing: Icon(
                    todo.completed ? Icons.check_circle : Icons.radio_button_unchecked,
                    color: todo.completed ? Colors.green : Colors.grey,
                  ),
                ),
              );
            },
          ),
        ),
      )),
      floatingActionButton: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          FloatingActionButton(
            heroTag: "network_demo",
            onPressed: () => Get.toNamed('/network-demo'),
            tooltip: '网络演示',
            child: const Icon(Icons.cloud),
          ),
          const SizedBox(height: 16),
          Obx(() => FloatingActionButton(
            heroTag: "main_action",
            onPressed: controller.pageState.hasError ? controller.retryLoad : controller.refreshTodos,
            tooltip: controller.pageState.hasError ? '重试' : '刷新',
            child: Icon(controller.pageState.hasError ? Icons.refresh : Icons.add),
          )),
        ],
      ),
    );
  }
}